<template>
	<view>
		<view class="" style="padding:20rpx;">
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="200" label="分类名称"><u-input v-model="form.name" /></u-form-item>
				<u-form-item label-width="200" label="分类描述"><u-input v-model="form.categoryDesc"
						type="textarea" /></u-form-item>
				<u-form-item label-width="200" label="排序"><u-input v-model="form.sort" type="number" /></u-form-item>
			</u-form>
			<view class="" style="margin-top:100rpx;">
				<u-button type="primary" @click="finsh">确定</u-button>
			</view>

		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: '',
					categoryDesc: '',
					sort: ''
				}
			}
		},
		onLoad(e) {
			console.log(e);
			if(e.id){
				console.log("你正在进行编辑");
				this.$set(this.form,'id',e.id)
				console.log(this.form);
				this.getClassInfo()
				
			}
		},
		methods: {
			finsh() {
				if(this.form.id){
					this.$u.put(`life/goods/category/${ this.form.id ? 'update' : 'save'}`, this.form).then(res => {
						console.log(res);
						if (res.success) {
							this.$refs.uToast.show({
								title: "修改成功",
								type: 'success',
							})
							setTimeout(()=>{
								uni.navigateBack(
								{
									delta:1
								})
							},1000)
						}
					})
				}else{
					this.$u.post(`life/goods/category/${ this.form.id ? 'update' : 'save'}`, this.form).then(res => {
						console.log(res);
						if (res.success) {
							this.$refs.uToast.show({
								title: "添加成功",
								type: 'success',
							})
							setTimeout(()=>{
								uni.navigateBack(
								{
									delta:1
								})
							},1000)
						}
					})
				}
				
			},
			getClassInfo(){
				this.$u.get("life/goods/category/info/" + this.form.id, {}).then(res => {
					console.log("我是获取分类信息");
					console.log(res);
					this.form.name = res.data.name,
					this.form.categoryDesc = res.data.categoryDesc
					this.form.sort = res.data.sort
				})
			},
		}
	}
</script>

<style>

</style>